package com.hmkj.web.controller.simulation;

import com.hmkj.common.entity.ResultEntity;
import com.hmkj.common.utils.PriceHelper;
import com.hmkj.core.constant.CacheID;
import com.hmkj.core.model.simulation.SimulationRank;
import com.hmkj.core.po.account.AccountSimulation;
import com.hmkj.core.po.simulation.Subscribe;
import com.hmkj.core.po.user.User;
import com.hmkj.core.service.account.AccountSimulationService;
import com.hmkj.core.service.simulation.OrderSimulationService;
import com.hmkj.core.service.simulation.SubscribeService;
import com.hmkj.web.controller.GenericController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * ......................我佛慈悲......................
 * ......................_oo0oo_.......................
 * .....................o8888888o......................
 * .....................88" . "88......................
 * .....................(| -_- |)......................
 * .....................0\  =  /0......................
 * ...................___/`---'\___....................
 * ..................' \\|     |// '...................
 * ................/ \\|||  :  |||// \.................
 * .............../ _||||| -卍-|||||- \................
 * ..............|   | \\\  -  /// |   |...............
 * ..............| \_|  ''\---/''  |_/ |...............
 * ..............\  .-\__  '-'  ___/-. /...............
 * ............___'. .'  /--.--\  `. .'___.............
 * .........."" '<  `.___\_<|>_/___.' >' ""............
 * ........| | :  `- \`.;`\ _ /`;.`/ - ` : | |.........
 * ........\  \ `_.   \_ __\ /__ _/   .-` /  /.........
 * ....=====`-.____`.___ \_____/___.-`___.-'=====......
 * ......................`=---='.......................
 * ....................................................
 * .................佛祖开光 ,永无BUG...................
 */
@Api(description = "模拟账户相关")
@Slf4j
@RestController
@RequestMapping(GenericController.MEMBER_URL + "/simulation/account")
public class AppAccountSimulationController extends GenericController<Integer, AccountSimulation> {

    @Resource
    private AccountSimulationService accountSimulationService;
    @Resource
    private OrderSimulationService orderSimulationService;
    @Resource
    private SubscribeService subscribeService;

    @ApiOperation(value = "用户模拟账户信息", notes = "用户模拟账户信息")
    @ResponseBody
    @RequestMapping(value = "/index", method = RequestMethod.POST)
    public ResultEntity index() throws Exception{
        User user = getLoginUser();
        AccountSimulation accountSimulation = accountSimulationService.selectOne(new AccountSimulation(t->{
            t.setUserId(user.getId());
            t.setDelFlag(AccountSimulation.DELFLAG.NORMAL.code);
        }));
        //计算总盈亏
        BigDecimal profitCount = orderSimulationService.getProfitCountByUser(user.getId());
        return ok(m->{
            m.put("accountSimulation",accountSimulation);
            m.put("totalProfit",profitCount);
        });
    }

    @ApiOperation(value = "炒股排行个人信息", notes = "炒股排行个人信息")
    @ResponseBody
    @RequestMapping(value = "/rankInfo", method = RequestMethod.POST)
    public ResultEntity rankPerson() throws Exception{
        User user = getLoginUser();
        Map<String, Object> data = new HashMap<>();
        data.put("name", user.getUserName());
        data.put("photo",user.getAvatarUrl());
        AccountSimulation simulation = accountSimulationService.selectOne(new AccountSimulation(t->{
            t.setUserId(user.getId());
        }));
        if (PriceHelper.isGreaterThan(simulation.getAvailable(), BigDecimal.valueOf(10000))){
            data.put("money", PriceHelper.format(simulation.getAvailable().multiply(new BigDecimal("0.0001"))) + "万元");
        }else {
            data.put("money", PriceHelper.format(simulation.getAvailable()) + "元");
        }

        //月排名
        List<SimulationRank> ranks = redisService.getList(CacheID.SIMULATION_RANK_PREFIX, SimulationRank.class);
        if (ranks != null && ranks.size() > 0) {
            for (int i = 0; i < ranks.size(); i++) {
                if (Objects.equals(ranks.get(i).getUserId(), user.getId())) {
                    data.put("rank", String.valueOf(i + 1));
                    break;
                } else {
                    data.put("rank", "--");
//                    break;
                }
            }
        } else {
            data.put("rank", "--");
        }
        Subscribe subscribe = subscribeService.selectOne(new Subscribe(t->{
            t.setUserId(user.getId());
        }));
        data.put("id", subscribe.getId());
        return ok(data);
    }


}
